package org.goldfish.cookied_chatroom02.model;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.goldfish.cookied_chatroom02.entity.UserBean;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.InputStream;

public interface RegisterManager extends LoginParams {
    /**
     * used in post
     *
     * @param session {@code Session} from {@code HttpRequest}  containing the {@code UserBean} <br/>
     *                i.e.
     * @return {@code null} if the info is {@code null} or cannot be parsed
     * otherwise the info parsed out
     */
    UserBean getUserBeanFromSession(HttpSession session);

    /**
     * @param stream stream to read info from
     * @return {@code RegisteringUserBean} containing error message<br/>
     * if the info is {@code null} or cannot be parsed<br/>
     * otherwise the info parsed out
     */
    @NotNull
    UserBean getUserBeanFromPostInputStream(InputStream stream);

    /**
     * @param cookies stream to read info from
     * @return {@code null} if the cookies array is {@code null} or cannot be parsed
     * otherwise the info parsed out
     */
    @Nullable
    UserBean getUserBeanFromCookies(Cookie[] cookies);

    /**
     * @return {@code false} if response or bean is {@code null} <br/>or any exception occurs<br/>
     * otherwise {@code true}
     */
    boolean writeUserBeanToCookie(@NotNull HttpServletResponse response, @NotNull UserBean userBean);

    /**
     * @return {@code false} if bean is {@code null} or any {@code SQLException} occurs<br/>
     * otherwise {@code true}
     */
    boolean writeUserBeanToDB(@NotNull UserBean userBean);

    void warnOverwriteExistingUser(HttpServletResponse response, UserBean userBean);

    /**
     * add message to {@code response} showing what userinfo caused what failure
     *
     * @param message the failure info
     * @param request
     */

    void warn(String message, HttpServletRequest request);

    /**
     * find the dispatcher to the registration page
     *
     * @return the dispatcher
     */
    RequestDispatcher register(HttpServletRequest request);

    /**
     * find the dispatcher to the login page
     *
     * @return the dispatcher
     */
    RequestDispatcher login(HttpServletRequest request);

}
